﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class TQuestionController : Controller
    {
        //
        // GET: /TQuestion/

        public ActionResult Index()
        {
            return View();
        }

		#region RegionMultiAnswerQuestion

		/// <summary>
		/// select all  multi answer question/by campaign
		/// </summary>
		/// <returns></returns>
		public ActionResult GetQuestion ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["client_id"], out idCampaign);
			TQuestionContainer container = TQuestionModels.GetQuestionArray(idCampaign);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get selected multi answer question infos(used for edit)
		/// </summary>
		/// <returns></returns>
		public ActionResult GetQuestionById ()
		{
			decimal idQuestion = 0;
			Decimal.TryParse(Request.Params["iRec"], out idQuestion);

			TQuestionContainer container = TQuestionModels.GetQuestionArrayById(idQuestion);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// add/modify selected multi answer question
		/// </summary>
		/// <returns></returns>
		public ActionResult EditQuestion ()
		{
			decimal idQuestion = 0;
			Decimal.TryParse(Request.Params["IdQuestion"], out idQuestion);

			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);

			TQuestionModels question = new TQuestionModels();
			question.IdCampaign = idCampaign;
			question.IdQuestion = idQuestion;
			question.QuestionName = Request.Params["QuestionName"];

			string messageBack = null;

			try
			{
				if (idQuestion == 0)
				{
					TQuestionModels.AddQuestion(question);
					messageBack = "Intrebarea a fost adaugata cu succes!";
				}
				else
				{
					if (idQuestion != 0)
					{
						TQuestionModels.EditQuestion(question);
						messageBack = "Intrebarea a fost modificata cu succes!";
					}
				}
				return Json(new { success = true, message = messageBack });
			}
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume!";
				}
				return Json(new { success = true, description = messageBack });
			}
		}
		#endregion

		#region RegionFreeAnswerQuestion

		/// <summary>
		/// select all free answer questions/by campaign
		/// </summary>
		/// <returns></returns>
		public ActionResult GetFQuestion ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["client_id"], out idCampaign);
			TQuestionContainer container = TQuestionModels.GetFQuestionArray(idCampaign);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// select free answer question infos for a selected question(for update)
		/// </summary>
		/// <returns></returns>
		public ActionResult GetFQuestionById ()
		{
			decimal idFQuestion = 0;
			Decimal.TryParse(Request.Params["iRec"], out idFQuestion);

			TQuestionContainer container = TQuestionModels.GetFQuestionArrayById(idFQuestion);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// add/modify free answer question
		/// </summary>
		/// <returns></returns>
		public ActionResult EditFQuestion ()
		{
			decimal idFQuestion = 0;
			Decimal.TryParse(Request.Params["IdFQuestion"], out idFQuestion);

			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);

			TQuestionModels question = new TQuestionModels();
			question.IdCampaign = idCampaign;
			question.IdFQuestion = idFQuestion;
			question.FQuestionName = Request.Params["FQuestionName"];

			string messageBack = null;

			try
			{
				if (idFQuestion == 0)
				{
					TQuestionModels.AddFQuestion(question);
					messageBack = "Intrebarea a fost adaugata cu succes!";
				}
				else
				{
					if (idFQuestion != 0)
					{
						TQuestionModels.EditFQuestion(question);
						messageBack = "Intrebarea a fost modificata cu succes!";
					}
				}
				return Json(new { success = true, message = messageBack });
			}
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume!";
				}
				return Json(new { success = true, description = messageBack });
			}
		}
		#endregion

		#region RegionAnswer

		/// <summary>
		/// select all answers for a selected multi answer question
		/// </summary>
		/// <returns></returns>
		public ActionResult GetAnswer ()
		{
			decimal idQuestion = 0;
			Decimal.TryParse(Request.Params["tq_id"], out idQuestion);
			TQuestionContainer container = TQuestionModels.GetAnswerArray(idQuestion);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get infos for a selected  answer 
		/// </summary>
		/// <returns></returns>
		public ActionResult GetAnswerById ()
		{
			decimal idAnswer = 0;
			Decimal.TryParse(Request.Params["iRec"], out idAnswer);

			TQuestionContainer container = TQuestionModels.GetAnswerArrayById(idAnswer);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// select answers by question id
		/// </summary>
		/// <returns></returns>
		public ActionResult GetAnswerByIdQuestion ()
		{
			decimal idQuestion = 0;
			Decimal.TryParse(Request.Params["IdQuestion"], out idQuestion);

			TQuestionContainer container = TQuestionModels.GetAnswerArrayByQuestion(idQuestion);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get answer for a selected contact
		/// </summary>
		/// <returns></returns>
		public ActionResult GetAnswerByContact ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);
			decimal idContact = 0;
			Decimal.TryParse(Request.Params["IdContact"], out idContact);

			TQuestionModels data = new TQuestionModels();
			data.IdCampaign = idCampaign;
			data.IdContact = idContact;

			TQuestionContainer container = TQuestionModels.GetAnswerArrayContact(data);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get free answer by contact
		/// </summary>
		/// <returns></returns>
		public ActionResult GetFreeAnswerByContact ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);
			decimal idContact = 0;
			Decimal.TryParse(Request.Params["IdContact"], out idContact);

			TQuestionModels data = new TQuestionModels();
			data.IdCampaign = idCampaign;
			data.IdContact = idContact;

			TQuestionContainer container = TQuestionModels.GetFreeAnswerArrayContact(data);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// add/modify answer
		/// </summary>
		/// <returns></returns>
		public ActionResult EditAnswer ()
		{
			decimal idAnswer = 0;
			Decimal.TryParse(Request.Params["IdAnswer"], out idAnswer);

			decimal idQuestion = 0;
			Decimal.TryParse(Request.Params["tq_id"], out idQuestion);

			TQuestionModels answer = new TQuestionModels();
			answer.IdAnswer = idAnswer;
			answer.IdQuestion = idQuestion;
			answer.AnswerName = Request.Params["AnswerName"];

			string messageBack = null;

			try
			{
				if (idAnswer == 0)
				{
					if (idQuestion != 0)
					{
						TQuestionModels.AddAnswer(answer);
						messageBack = "Raspunsul a fost adaugat cu succes!";
					}
					else
						if(idQuestion==0)
					{
						messageBack = "Selectati o intrebare!";
					}
				}
				else
				{
					if (idAnswer != 0)
					{
						TQuestionModels.EditAnswer(answer);
						messageBack = "Raspunsul a fost modificat cu succes!";
					}
				}
				return Json(new { success = true, message = messageBack });
			}
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume!";
				}
				return Json(new { success = true, description = messageBack });
			}
		}
		#endregion
	}
}
